package br.com.manutencaocarro.dao;

import java.sql.Date;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import br.com.manutencaocarro.modelo.Peca;

public class PecaDAO
{
	private DatabaseHelper helper;
	
	public PecaDAO(Context context ) {
		helper = new DatabaseHelper(context);
	}

	public void cadastraPeca(Peca peca) 
	{
		ContentValues values = new ContentValues();
		
		new DatabaseHelper.Peca();
		values.put( DatabaseHelper.Peca.ID_CARRO, peca.getIdCarro() );
		values.put( DatabaseHelper.Peca.ID_LOJA, peca.getIdLoja() );
		values.put( DatabaseHelper.Peca.NOME_PECA, peca.getNomePeca() );
		values.put( DatabaseHelper.Peca.QUANTIDADE, peca.getQtdPecas() );
		values.put( DatabaseHelper.Peca.UNIDADE, peca.getUnidade() );
		values.put( DatabaseHelper.Peca.VALOR_PECA, peca.getValorPeca() );
		values.put( DatabaseHelper.Peca.DATA_COMPRA, peca.getDataCompra().toString() );
		
		helper.getWritableDatabase().insert( DatabaseHelper.Peca.TABELA, null, values );
	}

	public List<Peca> getLista( Integer idCarro )
	{
		String[] args = { idCarro.toString() };
		Cursor cursor = helper.getWritableDatabase().query( DatabaseHelper.Peca.TABELA, DatabaseHelper.Peca.COLUNAS, "id_carro=?", args, null, null, "dataCompra DESC" );
		
		ArrayList<Peca> pecas = new ArrayList<>();
		
		while( cursor.moveToNext() )
		{
			Peca peca = new Peca();
			peca.setId( cursor.getInt(0) );
			peca.setNomePeca( cursor.getString(1) );
			peca.setQtdPecas( cursor.getLong(2) );
			peca.setUnidade( cursor.getInt(3) );
			peca.setValorPeca( cursor.getFloat(4) );
			peca.setDataCompra( new Date( cursor.getLong(4) ) );
			peca.setIdCarro( cursor.getInt(5) );
			peca.setIdLoja( cursor.getInt(6) );
			
			pecas.add(peca);
		}
		
		return pecas;
	}

	@SuppressLint("DefaultLocale")
	public List<Peca> getLista( )
	{
		HashMap<String, String> months = new HashMap<>();
		months.put( "JAN", "1" );
		months.put( "FEV", "2" );
		months.put( "FEB", "2" );
		months.put( "MAR", "3" );
		months.put( "ABR", "4" );
		months.put( "APR", "4" );
		months.put( "MAI", "5" );
		months.put( "MAY", "5" );
		months.put( "JUN", "6" );
		months.put( "JUL", "7" );
		months.put( "AGO", "8" );
		months.put( "AUG", "8" );
		months.put( "SET", "9" );
		months.put( "SEP", "9" );
		months.put( "OUT", "10" );
		months.put( "OCT", "10" );
		months.put( "NOV", "11" );
		months.put( "DEZ", "12" );
		months.put( "DEC", "12" );
		
		Cursor cursor = helper.getWritableDatabase().query( DatabaseHelper.Peca.TABELA, DatabaseHelper.Peca.COLUNAS, null, null, null, null, "dataCompra DESC" );
		ArrayList<Peca> pecas = new ArrayList<>();
		
		String day;
		String month;
		String year;
		String date;
		GregorianCalendar calendar;
		while( cursor.moveToNext() )
		{
			Peca peca = new Peca();
			peca.setId( cursor.getInt(0) );
			peca.setNomePeca( cursor.getString(1) );
			peca.setQtdPecas( cursor.getLong(2) );
			peca.setUnidade( cursor.getInt(3) );
			peca.setValorPeca( cursor.getFloat(4) );
			
			date = cursor.getString(5);
			
			if( date.matches("\\d{4}-\\d{2}-\\d{2}") )
			{
				day = cursor.getString(5).split( "-" )[2];
				month = cursor.getString(5).split( "-" )[1];
				year = cursor.getString(5).split( "-" )[0];
			} else{
				day = cursor.getString(5).split( " " )[2];
				month = cursor.getString(5).split( " " )[1];
				year = cursor.getString(5).split( " " )[5];
			}
			
			if( !month.equals(null) && !month.matches("\\d{2}") )
				month = months.get(month.toUpperCase());
			
			calendar = new GregorianCalendar(Integer.valueOf(year), Integer.valueOf(month), Integer.valueOf(day));
			calendar.add( GregorianCalendar.MONTH, -1 );
			peca.setDataCompra( calendar.getTime() );
			peca.setIdCarro( cursor.getInt(6) );
			peca.setIdLoja( cursor.getInt(7) );
			
			pecas.add(peca);
		}
		
		return pecas;
	}
	
	public void close(){
		helper.close();
	}

	public void excluiPeca(Integer id) {
		String[] args = { id.toString() };
		helper.getWritableDatabase().delete( DatabaseHelper.Peca.TABELA, DatabaseHelper.Peca.ID_PECA + "=?", args  );
	}

	public void atualiza(Peca pecaSelecionada) {
		String[] args = { pecaSelecionada.getId().toString() };
		ContentValues values = new ContentValues();
		values.put( DatabaseHelper.Peca.ID_PECA, pecaSelecionada.getId() );
		values.put( DatabaseHelper.Peca.ID_LOJA, pecaSelecionada.getIdLoja() );
		values.put( DatabaseHelper.Peca.NOME_PECA, pecaSelecionada.getNomePeca() );
		values.put( DatabaseHelper.Peca.QUANTIDADE, pecaSelecionada.getQtdPecas() );
		values.put( DatabaseHelper.Peca.UNIDADE, pecaSelecionada.getUnidade() );
		values.put( DatabaseHelper.Peca.VALOR_PECA, pecaSelecionada.getValorPeca() );
		values.put( DatabaseHelper.Peca.DATA_COMPRA, pecaSelecionada.getDataCompra().toString() );
		values.put( DatabaseHelper.Peca.ID_CARRO, pecaSelecionada.getIdCarro() );
		
		helper.getWritableDatabase().update( DatabaseHelper.Peca.TABELA, values, DatabaseHelper.Peca.ID_PECA + "= ?", args  );
	}
}